/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ar.com.neolokura.travian.helper.troop.admin;

import ar.com.neolokura.travian.helper.troop.entity.TroopDetail;
import ar.com.neolokura.travian.helper.bo.page.SendTroopsPageObject;
import ar.com.neolokura.travian.helper.bo.object.TroopSenderObject;
import ar.com.neolokura.travian.helper.browser.admin.BrowserAdmin;
import ar.com.neolokura.travian.helper.constant.TravianWebTree;
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import org.apache.log4j.Logger;

/**
 *
 * @author gustavo
 */
public class TroopAdmin {

    private static Logger logger = Logger.getLogger(TroopAdmin.class.getSimpleName());
    private static TroopAdmin INSTANCE = null;
    private WebClient webClient;

    // Private constructor suppresses 
    private TroopAdmin() {
        BrowserAdmin browserAdmin = BrowserAdmin.getInstance();
        webClient = browserAdmin.getWebClientInstance();
        logger.info(">> TroopAdmin started...");
    }

    // creador sincronizado para protegerse de posibles problemas  multi-hilo
    // otra prueba para evitar instanciación múltiple 
    private synchronized static void createInstance() {
        if (INSTANCE == null) {
            INSTANCE = new TroopAdmin();
        }
    }

    public static TroopAdmin getInstance() {
        if (INSTANCE == null) {
            createInstance();
        }
        return INSTANCE;
    }

    public void sendTroops(TroopSenderObject senderObject) throws Exception {

        if (senderObject == null) {
            return;
        }

        if (senderObject.getX() == null || senderObject.getY() == null) {
            throw new Exception("No X or Y Parameters are set");
        }
        if (senderObject.getSendType() == null) {
            throw new Exception("No Attack type selected");
        }

        TravianWebTree travianWeb = new TravianWebTree();
        HtmlPage page = webClient.getPage(travianWeb.getSendTroops());

        SendTroopsPageObject pageObject = new SendTroopsPageObject(page);

        pageObject.setT1(senderObject.getT1());
        pageObject.setT2(senderObject.getT2());
        pageObject.setT3(senderObject.getT3());
        pageObject.setT4(senderObject.getT4());
        pageObject.setT5(senderObject.getT5());
        pageObject.setT6(senderObject.getT6());
        pageObject.setT7(senderObject.getT7());
        pageObject.setT8(senderObject.getT8());
        pageObject.setT9(senderObject.getT9());
        pageObject.setT10(senderObject.getT10());

        pageObject.setX(senderObject.getX());
        pageObject.setY(senderObject.getY());

        pageObject.setAttackType(senderObject.getSendType());


        page = pageObject.getPageBySubmitButton(); // Goes to next page

        pageObject = new SendTroopsPageObject(page);



        page = pageObject.getPageBySubmitButton(); // Goes to next page

    }
    
      public HashMap<String, TroopDetail> getIncomingTroops(ArrayList<Integer> villageIds) throws FailingHttpStatusCodeException, IOException{
        
        
        
        TroopParser parser = new TroopParser();
        TravianWebTree tree = new TravianWebTree();

        HashMap<String, TroopDetail> troops = new HashMap<String, TroopDetail>();



        for (Integer villageId : villageIds) {
            String link = tree.getRallyPoint(villageId);
            HtmlPage page = webClient.getPage(link);
            webClient.waitForBackgroundJavaScript(3000);
            HtmlElement div = page.getElementById("troopsOnTheWayTo");

            String incomingTable = div.asXml();

            HashMap<String, TroopDetail> villageTroops = parser.getIncomingTroops(incomingTable, villageId);
            troops.putAll(villageTroops);
        }

        return troops;
        
        
    }
}
